#include "CPlayer.h"
#include "CAnimationManager.h"
#include "SGD Wrappers/CSGD_Direct3D.h"
#include "SGD Wrappers/CSGD_TextureManager.h"
#include "SGD Wrappers/CSGD_FModManager.h"
#include "CObjectManager.h"
#include "SGD Wrappers/CSGD_DirectInput.h"
#include "CGame.h"
#include "CXInput.h"
#include "CGamePlayState.h"
#include "CGameOverState.h"
#include "SGD Wrappers/SGD_Math.h"
#include "Mail System.h"
#include "Deliverables.h"
#include "CHealthBar.h"
#include "CPlayer.h"
#include "CEventSystem.h"
#include "CWorldCollision.h"
#include "CParticleManager.h"
#include "CEmitter.h"
#include "CFife.h"

CPlayer::CPlayer(void)
{
	m_fElapsedTime = 0;
	m_fElapsedTime2 = 0;
	m_fElapsedTime3 = 0;
	m_fElapsedTime4 = 0;
	m_nType = HERO;
	SetPosX(100);
	SetPosY(100);
	SetVelX(0);
	SetVelY(0);
	SetMaxHP(100);
	SetCurrHP(99);
	SetHeight(25);
	SetPower(50);
	SetRange((float)(GetPower() / 2));
	SetWidth(25);
	SetSpeed(50);

	m_pAPlayer.nCurrAnimation = 1;
	m_pAPlayer.nCurrFrame = 0;
	m_pAPlayer.nTimer = 0;
	m_bIsFlipped = false;
	IsAttracting = false;
	IsRunning = false;
	pEmitter = NULL;
	pEmitterBlue = NULL;
	pEmitterGreen = NULL;
	pEmitterRed = NULL;
	pEmitterYellow = NULL;
	pEmitterBlue = CParticleManager::GetInstance()->AddEmitter((int)GetPosX(), (int)GetPosY(), "Resource/Particle Effects/attractBlue.rpe");
	pEmitterGreen = CParticleManager::GetInstance()->AddEmitter((int)GetPosX(), (int)GetPosY(), "Resource/Particle Effects/attractGreen.rpe");
	pEmitterRed = CParticleManager::GetInstance()->AddEmitter((int)GetPosX(), (int)GetPosY(), "Resource/Particle Effects/attractRed.rpe");
	pEmitterYellow = CParticleManager::GetInstance()->AddEmitter((int)GetPosX(), (int)GetPosY(), "Resource/Particle Effects/attractYellow.rpe");
}

CPlayer::~CPlayer(void)
{
	if(pEmitterBlue != NULL)
	{
		CParticleManager::GetInstance()->RemoveEmitter(pEmitterBlue);
		pEmitterBlue = NULL;
	}
	if(pEmitterGreen != NULL)
	{
		CParticleManager::GetInstance()->RemoveEmitter(pEmitterGreen);
		pEmitterGreen = NULL;
	}
	if(pEmitterRed != NULL)
	{
		CParticleManager::GetInstance()->RemoveEmitter(pEmitterRed);
		pEmitterRed = NULL;
	}
	if(pEmitterYellow != NULL)
	{
		CParticleManager::GetInstance()->RemoveEmitter(pEmitterYellow);

		pEmitterYellow = NULL;
	}
}
void CPlayer::Update(float fDelta)
{
	Input();
	CFife::GetInstance()->Update(fDelta);
	CBase::Update(fDelta);


	if(GetCurrHP() <= 0)
	{
		CGame::GetInstance()->PushState(CGameOverState::GetInstance());
	}

	if(GetVelX() != 0 || GetVelY() != 0)
		CAnimationManager::GetInstance()->Update(fDelta, &m_pAPlayer);
	else
		m_pAPlayer.nCurrFrame = 0;

	m_fElapsedTime += fDelta;
	m_fElapsedTime2 += fDelta;
	m_fElapsedTime3 += fDelta;
	m_fElapsedTime4 += fDelta;

	SetVelX(0);
	SetVelY(0);
	if(pEmitter != NULL)
	{
		pEmitter->SetPosX(GetPosX() - GetHalfWidth());
		pEmitter->SetPosY(GetPosY() - GetHalfHeight());
	}
	if(pEmitterBlue != NULL)
	{
		pEmitterBlue->SetPosX(GetPosX() - GetHalfWidth());
		pEmitterBlue->SetPosY(GetPosY() - GetHalfHeight());
	}
	if(pEmitterGreen != NULL)
	{
		pEmitterGreen->SetPosX(GetPosX() - GetHalfWidth());
		pEmitterGreen->SetPosY(GetPosY() - GetHalfHeight());
	}
	if(pEmitterRed != NULL)
	{
		pEmitterRed->SetPosX(GetPosX() - GetHalfWidth());
		pEmitterRed->SetPosY(GetPosY() - GetHalfHeight());
	}
	if(pEmitterYellow != NULL)
	{
		pEmitterYellow->SetPosX(GetPosX() - GetHalfWidth());
		pEmitterYellow->SetPosY(GetPosY() - GetHalfHeight());
	}

}

void CPlayer::Input()
{
	//////////////////////////////////////////////////////////////////////////////////////////
	//Xbox Controller Input
	/////////////////////////////////////////////////////////////////////////////////////////
	if(CXInput::GetInstance()->IsConnected())
	{	
		/////////////////////////////////////////////////////////////////////////////////////
		//Movement
		if(CXInput::GetInstance()->LStickY() >= 7850 && GetPosY() >= 0)
		{
			SetVelY(-(float)GetSpeed());
		}
		if(CXInput::GetInstance()->LStickY() <= -7850 && GetPosY() + GetHeight() <= CGame::GetInstance()->GetScreenHeight())
		{
			SetVelY((float)GetSpeed());
		}
		if(CXInput::GetInstance()->LStickX() <= -7850 && GetPosX() >= 0)
		{
			SetVelX(-(float)GetSpeed());
		}
		if(CXInput::GetInstance()->LStickX() >= 7850 && GetPosX() + GetWidth() <= CGame::GetInstance()->GetScreenWidth())
		{
			SetVelX((float)GetSpeed());
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_RIGHT_SHOULDER || CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_LEFT_SHOULDER)
		{
			if(!IsRunning)
			{
				IsRunning = true;
				SetSpeed(GetSpeed()*2);
			}
		}
		else
		{
			if(IsRunning)
			{
				IsRunning = false;
				SetSpeed(GetSpeed()/2);
			}
		}
		///////////////////////////////////////////////////////////////////////////////////


		///////////////////////////////////////////////////////////////////////////////////////////////////
		//Animations
		//////////////////////////////////////////////////////////////////////////////////////////////////
		if((CXInput::GetInstance()->LStickY() >= 7850 && CXInput::GetInstance()->LStickX() >= 7850) 
			|| (CXInput::GetInstance()->LStickY() >= 7850 && CXInput::GetInstance()->LStickX() <= -7850)
			|| (CXInput::GetInstance()->LStickY() <= -7850 && CXInput::GetInstance()->LStickX() >= 7850) 
			|| (CXInput::GetInstance()->LStickY() <= -7850 && CXInput::GetInstance()->LStickX() <= -7850))
		{
		}
		else if (CXInput::GetInstance()->LStickY() >= 7850)
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_UP)
					m_pAPlayer.ChangeAnimation(ALT_WALK_UP);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_UP)
					m_pAPlayer.ChangeAnimation(WALK_UP);
			}
			m_bIsFlipped = false;
		}
		else if (CXInput::GetInstance()->LStickY() <= -7850)
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_DOWN)
					m_pAPlayer.ChangeAnimation(ALT_WALK_DOWN);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_DOWN)
					m_pAPlayer.ChangeAnimation(WALK_DOWN);
			}
			m_bIsFlipped = false;
		}
		/////////////////////////////////////////////////////////////////
		// BUG FIX
		// Reference Bug # BB-017
		// Approved by:
		// BUG FIX START
		/////////////////////////////////////////////////////////////////
		else if(CXInput::GetInstance()->LStickX() <= -7850)
		{
			///////////////////////////////////////////////////////////////////////////
			// BUG FIX END  Reference # BB-001
			//////////////////////////////////////////////////////////////////////////

			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_SIDE)
					m_pAPlayer.ChangeAnimation(ALT_WALK_SIDE);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_SIDE)
					m_pAPlayer.ChangeAnimation(WALK_SIDE);
			}
			m_bIsFlipped = false;
		}
		else if (CXInput::GetInstance()->LStickX() >= 7850)
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_SIDE)
					m_pAPlayer.ChangeAnimation(ALT_WALK_SIDE);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_SIDE)
					m_pAPlayer.ChangeAnimation(WALK_SIDE);
			}
			m_bIsFlipped = true;
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_X)
		{
			pEmitterBlue->PlayEffect(true);	
		}
		if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_X))
		{
			pEmitterBlue->PlayEffect(false);
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A)
		{
			pEmitterGreen->PlayEffect(true);	
		}
		if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_A))
		{
			pEmitterGreen->PlayEffect(false);
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_B)
		{
			pEmitterRed->PlayEffect(true);		
		}
		if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_B))
		{
			pEmitterRed->PlayEffect(false);
		}
		if(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_Y)
		{
			pEmitterYellow->PlayEffect(true);	
		}
		if(!(CXInput::GetInstance()->PressButton() & XINPUT_GAMEPAD_Y))
		{
			pEmitterYellow->PlayEffect(false);
		}
	}
	else
	{
		//////////////////////////////////////////////////////////////////////
		//Keyboard Input
		/////////////////////////////////////////////////////////////////////
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) && GetPosY() >= 0)
		{
			SetVelY(-(float)GetSpeed());
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) && GetPosY() + GetHeight() <= CGame::GetInstance()->GetScreenHeight())
		{
			SetVelY((float)GetSpeed());
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A) && GetPosX() >= 0)
		{
			SetVelX(-(float)GetSpeed());
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_D) && GetPosX() + GetWidth() <= CGame::GetInstance()->GetScreenWidth())
		{
			SetVelX((float)GetSpeed());
		}
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//Animations
		//////////////////////////////////////////////////////////////////////////////////////////////////
		if((CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) && CSGD_DirectInput::GetInstance()->KeyDown(DIK_D)) 
			|| (CSGD_DirectInput::GetInstance()->KeyDown(DIK_W) && CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
			|| (CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) && CSGD_DirectInput::GetInstance()->KeyDown(DIK_D)) 
			|| (CSGD_DirectInput::GetInstance()->KeyDown(DIK_S) && CSGD_DirectInput::GetInstance()->KeyDown(DIK_A)))
		{
		}
		else if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_W))
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_UP)
					m_pAPlayer.ChangeAnimation(ALT_WALK_UP);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_UP)
					m_pAPlayer.ChangeAnimation(WALK_UP);
			}
			m_bIsFlipped = false;
		}
		else if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_S))
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_DOWN)
					m_pAPlayer.ChangeAnimation(ALT_WALK_DOWN);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_DOWN)
					m_pAPlayer.ChangeAnimation(WALK_DOWN);
			}
			m_bIsFlipped = false;
		}
		else if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_A))
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_SIDE)
					m_pAPlayer.ChangeAnimation(ALT_WALK_SIDE);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_SIDE)
					m_pAPlayer.ChangeAnimation(WALK_SIDE);
			}
			m_bIsFlipped = false;
		}
		else if (CSGD_DirectInput::GetInstance()->KeyDown(DIK_D))
		{
			if(CGamePlayState::GetInstance()->SecondLevel() == true)
			{
				if(m_pAPlayer.nCurrAnimation != ALT_WALK_SIDE)
					m_pAPlayer.ChangeAnimation(ALT_WALK_SIDE);
			}
			else
			{
				if(m_pAPlayer.nCurrAnimation != WALK_SIDE)
					m_pAPlayer.ChangeAnimation(WALK_SIDE);
			}
			m_bIsFlipped = true;
		}
		
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_LSHIFT))
		{
			if(!IsRunning && !IsAttracting)
			{
				IsRunning = true;
				SetSpeed(GetSpeed()*2);
			}
		}
		if(CSGD_DirectInput::GetInstance()->KeyReleased(DIK_LSHIFT))
		{
			if(IsRunning)
			{
				IsRunning = false;
				SetSpeed(GetSpeed()/2);
			}
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_1))
		{
			pEmitterBlue->PlayEffect(true);
			if(pEmitterGreen->GetPlay() == true)
				pEmitterGreen->PlayEffect(false);
			if(pEmitterRed->GetPlay() == true)
				pEmitterRed->PlayEffect(false);
			if(pEmitterYellow->GetPlay() == true)
				pEmitterYellow->PlayEffect(false);
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_2))
		{
			pEmitterGreen->PlayEffect(true);
			if(pEmitterBlue->GetPlay() == true)
				pEmitterBlue->PlayEffect(false);
			if(pEmitterRed->GetPlay() == true)
				pEmitterRed->PlayEffect(false);
			if(pEmitterYellow->GetPlay() == true)
				pEmitterYellow->PlayEffect(false);
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_3))
		{
			pEmitterRed->PlayEffect(true);
			if(pEmitterGreen->GetPlay() == true)
				pEmitterGreen->PlayEffect(false);
			if(pEmitterBlue->GetPlay() == true)
				pEmitterBlue->PlayEffect(false);
			if(pEmitterYellow->GetPlay() == true)
				pEmitterYellow->PlayEffect(false);
		}
		if(CSGD_DirectInput::GetInstance()->KeyDown(DIK_4))
		{
			pEmitterYellow->PlayEffect(true);
			if(pEmitterGreen->GetPlay() == true)
				pEmitterGreen->PlayEffect(false);
			if(pEmitterRed->GetPlay() == true)
				pEmitterRed->PlayEffect(false);
			if(pEmitterBlue->GetPlay() == true)
				pEmitterBlue->PlayEffect(false);
		}
	}

	pEmitterBlue->SetPosX(GetPosX());
	pEmitterBlue->SetPosY(GetPosY());
	pEmitterGreen->SetPosX(GetPosX());
	pEmitterGreen->SetPosY(GetPosY());
	pEmitterRed->SetPosX(GetPosX());
	pEmitterRed->SetPosY(GetPosY());
	pEmitterYellow->SetPosX(GetPosX());
	pEmitterYellow->SetPosY(GetPosY());	
}
void CPlayer::Render()
{
	////////////////////////////////////////////////////
	//	Render Collision Box
	CSGD_Direct3D::GetInstance()->SpriteEnd();

	//CSGD_Direct3D::GetInstance()->DrawRect(GetDrawRect(), 255,255,0);

	CSGD_Direct3D::GetInstance()->SpriteBegin();
	//	Render Health Bar
	CHealthBar::GetInstance()->Render(this);
	//
	////////////////////////////////////////////////////
	CAnimationManager::GetInstance()->Render((int)(CPlayer::GetInstance()->GetPosX() - CPlayer::GetInstance()->GetHalfWidth()) - CGame::GetInstance()->RectCamera().left,
		(int)(CPlayer::GetInstance()->GetPosY() - CPlayer::GetInstance()->GetHalfHeight()) - CGame::GetInstance()->RectCamera().top, 1, -1, m_pAPlayer, m_bIsFlipped);
}

CPlayer* CPlayer::GetInstance()
{
	static CPlayer instance;
	return &instance;
}

bool CPlayer::Collision(CBase *pBase)
{
	if(CBase::Collision(pBase) && this->GetType() != pBase->GetType())
	{
		if(pBase->GetType() == UNIT)
		{
			//CWorldCollision::GetInstance()->CollideWithTile(this, (CLevelCollision*)pBase);
			return true;
		}
		else if(pBase->GetType() == HERO)
		{
			CWorldCollision::GetInstance()->CollideWithTile(this, (CLevelCollision*)pBase);
			return true;
		}
		else if(pBase->GetType() == BULLET)
		{
			return true;
		}
		else if(pBase->GetType() == RESOURCE)
		{
			CWorldCollision::GetInstance()->CollideWithTile(this, (CLevelCollision*)pBase);	
			return true;
		}
		else if(pBase->GetType() == LEVEL_COLLISION)
		{
			CLevelCollision* temp = (CLevelCollision*)pBase;
			if(temp->CollisionType() == NORMAL_COLLISION)
				CWorldCollision::GetInstance()->CollideWithTile(this, (CLevelCollision*)pBase);
			return true;
		}
		else
			return false;
	}	
	else
		return false;
}
void CPlayer::HandleEvent(CEvent *pEvent)
{

}